package com.kingwood.algorithm.test;

/**
 * @Author 22025812
 * @Description: TODO
 * @Date: 2024/3/22 10:03
 * @Modified By：
 * @Version 1.0
 */
public class longestPalindrome {
    public static String longestPalindrome(String s) {
        int len = s.length();
        if (len < 2) {
            return s;
        }

        // 特殊用例判断
        char[] charArray = s.toCharArray();
        int maxLen = 1;
        int begin = 0;

        // dp[i][j] 表示 s[i, j] 是否是回文串
        boolean[][] dp = new boolean[len][len];
        for (int i = 0; i < len; i++) {
            dp[i][i] = true;
        }

        for (int j = 1; j < len; j++) {
            for (int i = 0; i < j; i++) {
                if (charArray[i] != charArray[j]) {
                    dp[i][j] = false;
                } else {
                    if (j - i < 3) {
                        dp[i][j] = true;
                    } else {
                        dp[i][j] = dp[i + 1][j - 1];
                    }
                }

                // 只要 dp[i][j] == true 成立，就表示子串 s[i..j] 是回文，此时记录回文长度和起始位置
                if (dp[i][j] && j - i + 1 > maxLen) {
                    maxLen = j - i + 1;
                    begin = i;
                }
            }
        }

        return s.substring(begin, begin + maxLen);
    }

    public static String longestPalindrome3(String s) {
        int len = s.length();
        if (len < 2) {
            return s;
        }

        boolean[][] dp = new boolean[len][len];
        int maxLen = 1;
        int begin = 0;

        for(int i=len-1;i>=0;i--) {
            for(int j=i;j<len;j++) {
                if(s.charAt(i) != s.charAt(j)) {
                    //首先得是保证两端相等
                    continue;
                }

                dp[i][j] = (j-i <= 2 || dp[i+1][j-1]);
                // 只要 dp[i][j] == true 成立，就表示子串 s[i..j] 是回文，此时记录回文长度和起始位置
                if (dp[i][j] && j - i + 1 > maxLen) {
                    maxLen = j - i + 1;
                    begin = i;
                }
            }
        }

        return s.substring(begin, begin + maxLen);
    }

    /**
     * 统计回文字符串的个数
     * 暴力枚举
     */
    public static int countSubstrings(String s) {
        int len = s.length();
        if (len < 2) {
            return 1;
        }

        int count = 0;
        for (int i = 0; i < len; i++) {
            for (int j = i + 1; j <= len; j++) {
                if (isPalindrome(s.substring(i, j))) {
                    count++;
                }
            }
        }

        return count;
    }

    public static Boolean isPalindrome(String s) {
        int len = s.length();
        if (len < 2) {
            return true;
        }

        char[] charArray = s.toCharArray();
        for (int i = 0; i < len / 2; i++) {
            if (charArray[i] != charArray[len - 1 - i]) {
                return false;
            }
        }

        return true;
    }

    public static int countSubstrings2(String s) {
        int len = s.length();
        if (len < 2) {
            return 1;
        }

        int count = 0;
        boolean[][] dp = new boolean[len][len];

        for(int i=len-1;i>=0;i--) {
            for(int j=i;j<len;j++) {
                if(s.charAt(i) != s.charAt(j)) {
                    //首先得是保证两端相等
                    continue;
                }

                dp[i][j] = (j-i <= 2 || dp[i+1][j-1]);
                if(dp[i][j]) {
                    count++;
                }
            }
        }

        return count;
    }

    public static void main(String[] args) {
        // String s = "babad";
        String s = "cbbd";
        String res = longestPalindrome(s);
        System.out.println(res);
    }
}
